/*
 * @Author: tanjunyi@doctorwork.com
 * @Date: 2020-06-18 07:41:50
 * @LastEditors: tanjunyi@doctorwork.com
 * @LastEditTime: 2020-06-25 15:52:12
 * @Description:
 */
/**
 * 判断当前数据是不是对象
 */
export const isObject = (data) => {
	return (typeof data === 'object') && (data != null)
}

const LIFICYCLE_HOOKS = [
	'beforeCreate',
	'created',
	'mounted',
	'beforUpdate',
	'updated'
];
let strats = {};
function mergeHook(parentVal, childValue) {
	if (childValue) {
		if (parentVal) {
			return parentVal.concat(childValue)
		} else {
			return [childValue]
		}
	} else {
		return parentVal;
	}
}
LIFICYCLE_HOOKS.forEach(hook => {
	strats[hook] = mergeHook
})

export function def(data, key, value) {
	Object.defineProperty(data, key, {
		enumerable: false,
		configurable: false,
		value
	})
}

export function mergeOptions(parent, chiled) {
	const options = {};
	for (let key in parent) {
		mergeField(key);
	}

	for (let key in chiled) {
		if (!parent.hasOwnProperty(key)) {
			mergeField(key)
		}
	}

	function mergeField(key) {
		if (strats[key]) {
			options[key] = strats[key](parent[key], chiled[key]);
		} else if (isObject(parent[key]) && isObject(chiled[key])) {
			options[key] = {
				...parent[key],
				...chiled[key]
			}
		} else {

			options[key] = chiled[key] || parent[key]
		}
	}
	return options;
}